package com.herench.bs.handler;

import com.herench.bs.agent.IPersonAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.utils.IdCardUtils;
import com.herench.bs.utils.NameUtils;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/12/27.
 */

@Component
@Handler(code = "100217" ,name="添加就诊人")
public class AddClinicPersonHandler extends AbstractMessageHandler {

    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        Assert.hasLength(channelCode, "sysCode 不可为空");
        String user = requestMessage.getArgsPropertyStringValue("user");
        Assert.hasLength(user, "user 不可为空");
        String idCard = requestMessage.getArgsPropertyStringValue("idCard");
        Assert.hasLength(idCard, "idCard 不可为空");
        String name = requestMessage.getArgsPropertyStringValue("name");
        Assert.hasLength(name, "name 不可为空");
        String mobile = requestMessage.getArgsPropertyStringValue("mobile");
        Assert.hasLength(mobile, "mobile 不可为空");
        String verificationCode = requestMessage.getArgsPropertyStringValue("verificationCode");
        // verifyPhone 核实手机：1核实 ；0 不核实
        String verifyPhone = requestMessage.getArgsPropertyStringValue("verifyPhone");
        Assert.hasLength(verifyPhone, "verifyPhone 不可为空");
        if ("1".equals(verifyPhone)) {
            Assert.hasLength(verificationCode, "verificationCode 不可为空");
        }
        String defaultFlag = requestMessage.getArgsPropertyStringValue("defaultFlag");
        Assert.hasLength(defaultFlag, "defaultFlag 不可为空");
        if(IdCardUtils.isIDNumber(idCard)&&NameUtils.ChineseName(name)) {
            IPersonAgent agent = getCleverClinicContext().getApplicationContext().getBean(IPersonAgent.class);
            Map<String, Object> res = agent.addRelatedUserId(user, name, idCard, channelCode, mobile, verificationCode, defaultFlag, verifyPhone);
            if (res.get("code").equals("0")) {
                res.put("name", name);
                res.put("idCard", idCard);
                res.put("mobile", mobile);
                res.put("user", user);
            }

            responseMessage.setData(res);
            getPropertiesFromInvokerResultMap(responseMessage, res);
        } else{
            Map<String, Object> res=new HashMap<>();
            res.put(ResponseMessage.RES_CODE, "-1");
            res.put(ResponseMessage.MESSAGE, "请核对身份证号和姓名后再输入");
            responseMessage.setData(res);
            getPropertiesFromInvokerResultMap(responseMessage, res);
        }
        return responseMessage;
    }
}
